<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/OrdersDomain.php';
class OrdersDAO extends BaseMySQLDAO
{
	public function __construct($context) {
		parent::__construct($context, OrdersDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO orders(orderID,orderNumber,orderDate,userID,totalAmount,currency)
					VALUES	(
								".$obj->getOrderID().",
								".$obj->getOrderNumber().",
								'".$obj->getOrderDate()."',
								".$obj->getUserID().",
								".$obj->getTotalAmount().",
								'".$obj->getCurrency()."'
							)";
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM orders WHERE orderID=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE orders
				SET
					orders.orderNumber = ".$obj->getOrderNumber().",
					orders.orderDate = '".$obj->getOrderDate()."',
					orders.userID = ".$obj->getUserID().",
					orders.totalAmount = ".$obj->getTotalAmount().",
					orders.currency = '".$obj->getCurrency()."'
				 WHERE orderID=".$obj->getOrderID();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function findByOid($oid)
	{
		$ordersDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM orders WHERE orders.orderID = ".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$ordersDomain = new OrdersDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $ordersDomain;
	}

	public function findAll()
	{
		$ordersDomainArr = array();
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM orders";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		if($res)
		{
			while ($data = mysql_fetch_assoc($res))
			{
				$ordersDomain = new OrdersDomain($data);
				$ordersDomainArr[] = $ordersDomain;
			}
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $ordersDomain;
	}

}
?>